from sklearn.preprocessing import StandardScaler
import pandas as pd
import numpy as np # linear algebra
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV


def FEtrain_lr(train, printSwitch):
    NaN_col_names = ['Plasma_glucose_concentration', 'blood_pressure', 'Triceps_skin_fold_thickness', 'serum_insulin',
                     'BMI']
    train[NaN_col_names] = train[NaN_col_names].replace(0, np.NaN)
    if printSwitch == 1:
        print(train.isnull().sum())
    medians = train.median()
    train = train.fillna(medians)
    if printSwitch == 1:
        print(train.isnull().sum())

    #  get labels
    y_train = train['Target']
    X_train = train.drop(["Target"], axis=1)
    # 用于保存特征工程之后的结果
    feat_names = X_train.columns
    # 数据标准化
    # 初始化特征的标准化器
    ss_X = StandardScaler()
    # 分别对训练和测试数据的特征进行标准化处理
    X_train = ss_X.fit_transform(X_train)
    # 存为csv格式
    X_train = pd.DataFrame(columns=feat_names, data=X_train)
    train = pd.concat([X_train, y_train], axis=1)
    train.to_csv('FE_pima-indians-diabetes.csv', index=False, header=True)
    if printSwitch == 1:
        print(train.head())
    lr = LogisticRegression()
    loss = cross_val_score(lr, X_train, y_train, cv=5, scoring='neg_log_loss')
    loss_accura = cross_val_score(lr, X_train, y_train, cv=5, scoring='accuracy')

    print('loss of each fold is: ', -loss)
    print('cv loss is:', -loss.mean())
    print('loss_accura of each fold is: ', loss_accura)
    print('cv loss_accura is:', loss_accura.mean())

    penaltys = ['l1', 'l2']
    Cs = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
    tuned_parameters = dict(penalty=penaltys, C=Cs)

    lr_penalty = LogisticRegression(solver='liblinear')
    grid = GridSearchCV(lr_penalty, tuned_parameters, cv=5, scoring='neg_log_loss')
    grid.fit(X_train, y_train)
    print('grid best score is:', -grid.best_score_)
    print('grid best params is:', grid.best_params_)
